package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.text.TextUtils;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.user.UserInfo;
import com.tang.user.UserInfoManager;

import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.LoginContract;
import top.hidoctor.auscultation.net.req.LoginReq;
import top.hidoctor.auscultation.net.req.RegisterReq;
import top.hidoctor.auscultation.net.req.SendEmsReq;

public class LoginPresenter extends PresenterWrapper<LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(Context context, LoginContract.View view) {
        super(context, view);
    }

    @Override
    public void sendSmsCode(SendEmsReq req) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        PatchRequest patchRequest = new PatchRequest(map);
        add(mService.sendSms(patchRequest.getRequestBody())
                .compose(getTransformer())
                .subscribe(new SuccessConsumer<Object>(mView, mContext) {
                    @Override
                    public void onSuccess(Object response) {
                        mView.hideLoading();
                        mView.sendSmsSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));

    }

    @Override
    public void login(final LoginReq req) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        PatchRequest patchRequest = new PatchRequest(map);
        add(mService.login(patchRequest.getRequestBody())
                .compose(this.<UserInfo>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<UserInfo>>(mView, mContext) {

                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        mView.hideLoading();
                        if (TextUtils.isEmpty(response.getData().getAccessToken())) {
                            mView.completeInfo(response.getData().getUserId());
                        } else {
                            UserInfoManager.getInstance().setUserInfo(response.getData());
                            mView.loginSuc();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void register(RegisterReq req) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        PatchRequest patchRequest = new PatchRequest(map);
        add(mService.signup(patchRequest.getRequestBody())
                .compose(getTransformer())
                .subscribe(new SuccessConsumer<Object>(mView, mContext) {
                    @Override
                    public void onSuccess(Object response) {
                        mView.hideLoading();
                        mView.registerSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
